package sholmes.euler.problem1;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Calculate the sum of all natural numbers that are multiples of 3 and 5 
 * @author sholmes
 *
 */
public class SumofNaturals {
	private int maxThreshhold;
	private final int DEFAULT_MULTIPLE = 2;
	private Collection<Integer> multiples;
	
	public SumofNaturals(int aMaxThreshold){
		this.maxThreshhold = aMaxThreshold;
		multiples = new ArrayList<Integer>();
		multiples.add(DEFAULT_MULTIPLE);	
	}
	
	/**
	 * Default multiple is 2.  You must specify multiples as comma separated integers as an argument.  If multiples are
	 * 	specified the default is removed.
	 * 
	 * @param multipleValues - 
	 * @return Sum of Multiples to MAX_THRESHOLD
	 */
	public int getSumofNaturals(int... multipleValues){
		if(multipleValues.length >0 ){
			setMultiples(multipleValues);
		}
		int sum = 0;
		int valueAdded=0;
		
		//O(N^2) is ugly but makes this flexible 
		
		//Iterate over all numbers below the threshold
		for(int inc=0; inc < maxThreshhold; inc++){
			for(int aMultiple : multiples){
			
				//Check to verify the value has not been added.
				if(((inc%aMultiple) == 0) && inc != valueAdded){								
					sum+=inc;
					valueAdded=inc;
				}
			}
		}
		
		return sum;	
	}
	
	private void setMultiples(int[] multipleValues){
		//Remove the default value from the multiples array
		multiples.clear();
		for(int aMultiple : multipleValues){
			multiples.add(aMultiple);
		}				
	}	
}
